home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / pdcsrc.lzh / A68k / A68kmisc.c < prev    next >
C/C++ Source or Header  |  1990-04-19  |  31KB  |  1,123 lines

  1. /*------------------------------------------------------------------*/
  2. /*                                    */
  3. /*            MC68000 Cross Assembler                */
  4. /*                                    */
  5. /*        Copyright (c) 1985 by Brian R. Anderson            */
  6. /*                                    */
  7. /*             Miscellaneous routines - November 2, 1989        */
  8. /*                                    */
  9. /*   This program may be copied for personal, non-commercial use    */
  10. /*   only, provided that the above copyright notice is included        */
  11. /*   on all copies of the source code.  Copying for any other use   */
  12. /*   without the consent of the author is prohibited.            */
  13. /*                                    */
  14. /*------------------------------------------------------------------*/
  15. /*                                    */
  16. /*        Originally published (in Modula-2) in            */
  17. /*        Dr. Dobb's Journal, April, May, and June 1986.          */
  18. /*                                    */
  19. /*     AmigaDOS conversion copyright 1989 by Charlie Gibbs.        */
  20. /*                                    */
  21. /*------------------------------------------------------------------*/
  22.  
  23. #include <stdio.h>
  24. #include "A68kdef.h"
  25. #include "A68kglb.h"
  26.  
  27. char Sdata[MAXSREC];    /* S-record data */
  28. int  Sindex;        /* Index for Sdata */
  29. int  NumRExt, NumR32, NumR16, NumR8;
  30.  
  31. static char *errmsg[] = {
  32.     "--- Unknown error code ---",
  33.     "Alignment error.",
  34.     "No such op-code.",
  35.     "Duplicate Symbol.",
  36.     "Undefined Symbol.",
  37.     "Addressing mode not allowed here.",
  38.     "Error in operand format.",
  39.     "Error in relative branch.",
  40.     "Address mode error.",
  41.     "Operand size error.",
  42.     "END statement is missing.",
  43.     "Value must be absolute.",
  44.     "Relocatability error.",
  45.     "INCLUDE file cannot be opened.",
  46.     "Illegal forward reference.",
  47.     "Not supported in S-format.",
  48.     "This instruction needs a label.",
  49.     "Pass 1 / Pass 2 phase error.",
  50.     "ENDM statement is missing.",
  51.     "ENDC statement is missing.",
  52.     "Unmatched ENDC statement.",
  53.     "Too much DC data.",
  54.     "Too many SECTIONs.",
  55.     "Duplicate macro definition.",
  56.     "More than one label on this line.",
  57.     "End of string is missing.",
  58.     "Short displacement can't be zero.",
  59.     ""};
  60.  
  61. /* Functions */
  62. extern int  LineParts(), Instructions(), ObjDir();
  63. extern int  GetInstModeSize(), GetMultReg(), CountNest();
  64. extern int  ReadSymTab(), GetArgs(), GetAReg(), OpenIncl();
  65. extern long GetValue(), CalcValue();
  66. extern char *AddName(), *GetField();
  67. extern struct SymTab *NextSym();
  68. extern struct SymTab **HashIt();
  69.  
  70. long AddrBndW(), AddrBndL();
  71.  
  72.  
  73.  
  74. long AddrBndW (v) register long v;
  75. /* Advances "v" to the next word boundary. */
  76. {
  77.     register int i;
  78.  
  79.     if (v & 1L) {
  80.     AppendSdata (0L, 1);
  81.     v++;
  82.     }
  83.     return (v);
  84. }
  85.  
  86.  
  87.  
  88. long AddrBndL (v) register long v;
  89. /* Advances "v" to the next long-word boundary. */
  90. {
  91.     long templong;
  92.  
  93.     v = AddrBndW (v);        /* Bump to a word boundary first. */
  94.     if (v & 2L) {        /* If still not aligned, */
  95.     templong = NOP;        /*  generate a NOP. */
  96.     AppendSdata (templong, 2);
  97.     v += 2;
  98.     }
  99.     return (v);
  100. }
  101.  
  102.  
  103.  
  104. WriteListLine (f) struct fs *f;
  105. /* Writes one line to the Listing file, including Object Code. */
  106. {
  107.     register int i, j, printed;
  108.     long templong;
  109.     char macflag;
  110.     char tempstr[12];
  111.     int  dummy;
  112.  
  113.     if (!Pass2)
  114.     return;            /* Pass 2 only */
  115.     if (FwdShort && (ErrLim == 0)) {
  116.     DisplayLine (dummy);
  117.     printf ("A short branch can be used here.\n");
  118.     }
  119.     if (SuppList)
  120.     return;            /* Listing is suppresed. */
  121.     if (ErrLim == 0)
  122.     if ((Dir == Page) || (Dir == Space) || (Dir == Title)
  123.     || (Dir == DoList) || (Dir == NoList) || (ListOff))
  124.         return;        /* Don't print unless they have errors. */
  125.  
  126.     CheckPage (f, FALSE);    /* Print headings if necessary. */
  127.  
  128.     if (PrntAddr) {
  129.     if ((Dir == Equ) || (Dir == Set))
  130.         LongPut (f, ObjSrc, 3);    /* Equated value */
  131.     else
  132.         LongPut (f, AddrCnt, 3);    /* Current location */
  133.     if (!KeepTabs)
  134.         xputs (f, "  ");
  135.     } else
  136.     if (!KeepTabs)
  137.         xputs (f, "        ");    /* Don't print location. */
  138.     if (KeepTabs)
  139.     xputs (f, "\t");        /* Use tabs for spacing. */
  140.     printed = 8;            /* We've printed 8 positions. */
  141.     LongPut (f, ObjOp, nO);        /* Generated code */
  142.     printed += nO * 2;
  143.     if (nS != 0) {
  144.     xputs (f, " ");
  145.     LongPut (f, ObjSrc, nS);
  146.     printed+= nS * 2 + 1;
  147.     }
  148.     if (nD != 0) {
  149.     xputs (f, " ");
  150.     LongPut (f, ObjDest, nD);
  151.     printed += nD * 2 + 1;
  152.     }
  153.     if ((j = nX) > 0) {                /* String data */
  154.     if ((j * 2 + printed) > ObjMAX)
  155.         j = (ObjMAX - printed) / 2;
  156.     for (i = 0; i < j; i++) {
  157.         templong = ObjString[i];
  158.         LongPut (f, templong, 1);
  159.     }
  160.     printed += j * 2;
  161.     }
  162.     while (printed < ObjMAX) {
  163.     if (KeepTabs) {
  164.         xputs (f, "\t");
  165.         printed += 8;
  166.         printed &= ~7;
  167.     } else {
  168.         xputs (f, " ");
  169.         printed++;
  170.     }
  171.     }
  172.     if ((InFNum == 0) || (OuterMac == 0))
  173.     macflag = ' ';            /* Open code */
  174.     else if (InFNum > OuterMac)
  175.     macflag = '+';            /* Inner macro */
  176.     else if ((InFNum == OuterMac) && (Dir != MacCall))
  177.     macflag = '+';            /* Outermost macro */
  178.     else
  179.     macflag = ' ';            /* We're outside macros. */
  180.  
  181.     sprintf (tempstr, "  %5d%c", LineCount, macflag);
  182.     xputs (f, tempstr);
  183.     xputs (f, Line);
  184.     xputs (f, "\n");
  185.  
  186.     if (FwdShort && (ErrLim == 0))
  187.     xputs (f, "A short branch can be used here.\n");
  188.  
  189.     for (i = 0; i < ErrLim; i++) {    /* Write error messages. */
  190.     CheckPage (f, FALSE);
  191.     xputs (f, errmsg[ErrCode[i]]);
  192.     printed = strlen(errmsg[ErrCode[i]]);
  193.     while (printed < ObjMAX + 8) {
  194.         if (KeepTabs) {
  195.         xputs (f, "\t");
  196.         printed += 8;
  197.         printed &= ~7;
  198.         } else {
  199.         xputs (f, " ");
  200.         printed++;
  201.         }
  202.     }
  203.     for (j = 0; j < ErrPos[i]; j++) {
  204.         if (Line[j] == '\t') {
  205.         xputs (f, "\t");
  206.         printed += 8;
  207.         printed &= ~7;
  208.         } else {
  209.         xputs (f, " ");
  210.         printed++;
  211.         }
  212.     }
  213.     xputs (f, "^ ");            /* Error flag */
  214.     if (i == 0) {
  215.         if (InF->UPtr == 0)
  216.         xputs (f, InF->NPtr);        /* Module name */
  217.         else
  218.         xputs (f, "(user macro)");    /* We're in a user macro. */
  219.         sprintf (tempstr, " line %d", InF->Line);
  220.         xputs (f, tempstr);            /* Line number */
  221.     }
  222.     xputs (f, "\n");
  223.     }
  224. }
  225.  
  226.  
  227.  
  228. WriteSymTab (f) struct fs *f;
  229. /* Lists the symbol table in alphabetical order. */
  230. {
  231.     int  printhunk, i;
  232.     char *p;
  233.     char tempstr[24];
  234.     long templong;
  235.     register int j, k;
  236.     register struct SymTab **ss1, **ss2, *sym, **sortlim;
  237.     struct Ref *ref;
  238.  
  239.     if (NumSyms == 0)
  240.     return;            /* The symbol table is empty - exit. */
  241.  
  242. /* Build a sorted table of pointers to symbol table entries. */
  243.  
  244.     templong = NumSyms * sizeof (struct SymTab *);
  245.     SymSort = (struct SymTab **) malloc ((unsigned) templong);
  246.     if (SymSort == NULL) {
  247.     fprintf (stderr, "Not enough memory for symbol table sort!\n");
  248.     return;
  249.     }
  250.     sortlim = SymSort + NumSyms;
  251.     sym = SymChunk = SymStart;
  252.     sym++;
  253.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  254.     ss1 = SymSort;
  255.     while (sym) {
  256.     *ss1++ = sym;
  257.     sym = NextSym (sym);    /* Try for another symbol table entry. */
  258.     }
  259.     for (i = NumSyms / 2; i > 0; i /= 2) {        /* Shell sort */
  260.     for (ss1 = SymSort + i; ss1 < sortlim; ss1++) {    /*  (copied   */
  261.         for (ss2=ss1-i; ss2 >= SymSort; ss2 -= i) {    /*  from K&R) */
  262.         if (strcmp ((*ss2)->Nam, (*(ss2+i))->Nam) <= 0)
  263.             break;
  264.         sym = *ss2;
  265.         *ss2 = *(ss2+i);
  266.         *(ss2+i) = sym;
  267.         }
  268.     }
  269.     }
  270.  
  271. /* The table is now sorted - print the listing. */
  272.  
  273.     LnCnt = LnMax;            /* Skip to a new page. */
  274.     for (i = 0, ss1 = SymSort; i < NumSyms; i++) {
  275.     sym = *ss1++;
  276.     CheckPage (f, TRUE);
  277.  
  278.     p = sym->Nam;            /* Pointer to symbol */
  279.     if (sym->Flags & 8)
  280.         p++;            /* Skip blank preceding macro name. */
  281.     else if (sym->Flags & 0x10)
  282.         p += 6;            /* Skip hunk sequence number. */
  283.     sprintf (tempstr, "%-11s ", p);    /* Symbol or macro name */
  284.     xputs (f, tempstr);
  285.     if (strlen (p) > 11)        /* Long symbol - go to new line. */
  286.         if (KeepTabs)
  287.         xputs (f, "\n\t    ");
  288.         else
  289.         xputs (f, "\n            ");
  290.  
  291.     printhunk = FALSE;        /* Assume no hunk no. to print. */
  292.     if (sym->Defn == NODEF)
  293.         xputs (f, "  *** UNDEFINED *** ");
  294.     else if (sym->Flags & 4)
  295.         xputs (f, "  -- SET Symbol --  ");
  296.     else if (sym->Flags & 8) {
  297.         sprintf (tempstr, " +++ MACRO +++ %5d", sym->Defn);
  298.         xputs (f, tempstr);
  299.     } else if (sym->Flags & 0x10) {
  300.         j = (sym->Hunk & 0x3FFF0000L) >> 16;
  301.         if (j == HunkCode)
  302.         xputs (f, "  CODE    ");
  303.         else if (j == HunkData)
  304.         xputs (f, "  DATA    ");
  305.         else
  306.         xputs (f, "  BSS     ");
  307.         printhunk = TRUE;
  308.     } else if (sym->Flags & 0x20) {
  309.         sprintf (tempstr, "      %c%ld  ",
  310.         (sym->Val & 8L) ? 'A' : 'D', sym->Val & 7L);
  311.         xputs (f, tempstr);
  312.         printhunk = TRUE;
  313.     } else {
  314.         LongPut (f, sym->Val, 4);        /* Value */
  315.         xputs (f, "  ");
  316.         printhunk = TRUE;
  317.     }
  318.     if (printhunk) {
  319.         j = sym->Hunk & 0x00007FFFL;    /* Hunk number */
  320.         if (sym->Flags & 0x60)
  321.         xputs (f, " Reg");        /* Register or list */
  322.         else if (sym->Flags & 1)
  323.         xputs (f, " Ext");        /* External */
  324.         else if (j == ABSHUNK)
  325.         xputs (f, " Abs");        /* Absolute */
  326.         else {
  327.         sprintf (tempstr, "%4d", j);    /* Hunk number */
  328.         xputs (f, tempstr);
  329.         }
  330.         sprintf (tempstr," %5d",sym->Defn);    /* Statement number */
  331.         xputs (f, tempstr);
  332.     }
  333.     if (XrefList) {
  334.         xputs (f, "  ");
  335.         if (sym->Ref1 == NULL)
  336.         xputs (f, " *** UNREFERENCED ***");
  337.         else {
  338.         ref = sym->Ref1;
  339.         j = k = 0;
  340.         while (1) {
  341.             if (ref->RefNum[j] == 0)
  342.             break;
  343.             if (k >= 9) {
  344.             xputs (f, "\n");        /* New line */
  345.             if (KeepTabs)
  346.                 xputs (f, "\t\t\t\t  ");    /* 34 spaces */
  347.             else
  348.                 for (k = 0; k < 34; k++)
  349.                 xputs (f, " ");
  350.             k = 0;
  351.             }
  352.             sprintf (tempstr, "%5d", ref->RefNum[j]);
  353.             xputs (f, tempstr);
  354.             j++;
  355.             k++;
  356.             if (j < MAXREF)
  357.             continue;        /* Get the next slot. */
  358.             if ((ref = ref->NextRef) == 0)
  359.             break;            /* End of last entry */
  360.             j = 0;            /* Start the next entry. */
  361.         }
  362.         }
  363.     }
  364.     xputs (f, "\n");
  365.     }
  366.     free (SymSort);        /* Free the sort work area. */
  367.     SymSort = NULL;
  368. }
  369.  
  370.  
  371.  
  372. CheckPage (f, xhdr) struct fs *f; int xhdr;
  373. /* Checks if end of page reached yet -- if so, advances to next page. */
  374. {
  375.     register int printed;
  376.     char tempstr[12];
  377.  
  378.     LnCnt++;
  379.     if (LnCnt >= LnMax) {
  380.     PgCnt++;
  381.     if (PgCnt > 1)
  382.         xputs (f, "\f");        /* Skip to new page. */
  383.     xputs (f, TTLstring);        /* Title */
  384.     printed = strlen (TTLstring);
  385.     while (printed < 56)
  386.         if (KeepTabs) {
  387.         xputs (f, "\t");
  388.         printed += 8;
  389.         printed &= ~7;
  390.         } else {
  391.         xputs (f, " ");
  392.         printed++;
  393.         }
  394.     xputs (f, SourceFN);        /* File name */
  395.     if (KeepTabs)
  396.         xputs (f, "\t");
  397.     else
  398.         xputs (f, "        ");
  399.     sprintf(tempstr, "Page %d\n\n", PgCnt);    /* Page number */
  400.     xputs (f, tempstr);
  401.     LnCnt = 2;
  402.     if (xhdr) {
  403.         xputs (f, "Symbol       Value    Hunk  Line");
  404.         if (XrefList)
  405.         xputs (f, "   References");    /* Cross-reference */
  406.         xputs (f, "\n\n");
  407.         LnCnt += 2;
  408.     }
  409.     }
  410. }
  411.  
  412.  
  413.  
  414. StartSrec (f, idntname) struct fs *f; char *idntname;
  415. /* Writes object header record. */
  416. {
  417.     register long CheckSum, templong;
  418.     register char *s;
  419.  
  420.     if (SFormat) {
  421.     xputs (f, "S0");
  422.     templong = strlen (idntname) + 3;   /* extra for addr. & checksum */
  423.     LongPut (f, templong, 1);
  424.     CheckSum = templong;
  425.  
  426.     xputs (f, "0000");    /* Address is 4 digits, all zero, for S0. */
  427.  
  428.     s = idntname;
  429.     while (*s) {
  430.         templong = toupper (*s);
  431.         LongPut (f, templong, 1);
  432.         CheckSum += templong;
  433.         s++;
  434.     }
  435.     CheckSum = ~CheckSum;        /* Complement the checksum. */
  436.     LongPut (f, CheckSum, 1);
  437.     xputs (f, "\n");
  438.     } else {
  439.     templong = HunkUnit;
  440.     xputl (f, templong);
  441.     DumpName (f, idntname, 0L);
  442.     }
  443.     StartAddr = TempAddr = Sindex = 0;
  444.     NumRExt = NumR32 = NumR16 = NumR8 = 0;
  445. }
  446.  
  447.  
  448.  
  449. WriteSrecLine (f) struct fs *f;
  450. /* Transfers object code components to output buffer. */
  451. /* Moves long words or portions thereof. */
  452. {
  453.     register int i, j;
  454.     register long templong;
  455.  
  456.     if (HunkType == HunkBSS)
  457.     return;                /* No code in BSS hunk! */
  458.  
  459.     if (nO + nS + nD + nX) {        /* If we have object code... */
  460.     AppendSdata (ObjOp, nO);    /* Opcode */
  461.     AppendSdata (ObjSrc, nS);    /* Source */
  462.     AppendSdata (ObjDest, nD);    /* Destination */
  463.     for (i = 0; i < DupFact; i++) {
  464.         for (j = 0; j < nX; j++) {    /* String data */
  465.         templong = ObjString[j];
  466.         AppendSdata (templong, 1);
  467.         }
  468.     }
  469.     }
  470. }
  471.  
  472.  
  473.  
  474. AppendSdata (Data, n) register long Data; int n;
  475. /* If we are producing S-format records:
  476.      Transfers "n" low-order bytes from "Data" to the output buffer.
  477.      If the buffer becomes full, DumpSdata will be called to flush it.
  478.      S-records will also be broken on 16-byte boundaries.
  479.    If we are producing AmigaDOS format, data will be written
  480.      directly to Srec - we'll go back and fill in the hunk length
  481.      at the end of the hunk.  DumpSdata will never be called from here. */
  482. {
  483.     register int  i;
  484.     register char byte;
  485.     int dummy;
  486.  
  487.     if (!Pass2)
  488.     return;            /* Pass 2 only */
  489.     if (HunkType == HunkBSS)
  490.     return;            /* No data in BSS hunks! */
  491.  
  492.     if (HunkType == HunkNone) {        /* We're not in a hunk yet - */
  493.     DoSection ("", 0, "", 0, "", 0);    /* start a code hunk. */
  494.     MakeHunk = TRUE;
  495.     }
  496.  
  497.     if (OrgFlag) {        /* If we've had an ORG directive */
  498.     FixOrg (dummy);        /*  do necessary adjustments     */
  499.     OrgFlag = FALSE;    /*  to the object code file.     */
  500.     }
  501.  
  502.     Data <<= (4 - n) * 8;    /* Left-justify data. */
  503.  
  504.     for (i = 0; i < n; i++) {
  505.     byte = (char) (Data >> ((3 - i) * 8));
  506.     TempAddr++;
  507.     if (!SFormat) {
  508.         xputc (byte, &Srec);
  509.     } else {
  510.         Sdata[Sindex++] = byte;
  511.         if (((TempAddr & 0x0F) == 0) || (Sindex >= MAXSREC))
  512.         DumpSdata (&Srec);    /* Break S-record. */
  513.     }
  514.     }
  515. }
  516.  
  517.  
  518.  
  519. FixOrg (dummy) int dummy;
  520. /* Makes necessary adjustments to the object code file if an
  521.     ORG directive has been processed.  This routine is called
  522.     exclusively by AppendSdata and must only be called once for
  523.     each ORG encountered, when writing the next object code (if any). */
  524. {
  525.     register long templong;
  526.     register int  i;
  527.  
  528.     if (SFormat && (AddrCnt != TempAddr)) {    /* ORG in S-format -    */
  529.     DumpSdata (&Srec);            /*  dump current record */
  530.     StartAddr = TempAddr = AddrCnt;        /*  and start afresh.   */
  531.     }
  532.     if (AddrCnt < TempAddr) {        /* AmigaDOS backward ORG */
  533.     if (TempAddr > OrgHigh) {
  534.         LenPtr = NULL;
  535.         OrgHigh = TempAddr;    /* Save high address for return. */
  536.         if (Srec.Ptr > Srec.Buf)    /* Flush the buffer. */
  537.         write (Srec.fd, Srec.Buf, Srec.Ptr - Srec.Buf);
  538.         OrgSeek = lseek (Srec.fd, 0L, 1);    /* Remember position. */
  539.         lseek (Srec.fd,(AddrCnt & ~3L)-TempAddr,1);    /* New position */
  540.         if (AddrCnt    & 3L) {        /* If ORG isn't to long-word   */
  541.         read (Srec.fd, Srec.Buf, AddrCnt & 3L);    /*  move ahead */
  542.         lseek (Srec.fd, -(AddrCnt & 3L), 1);    /*  to keep    */
  543.         }                        /*  the buffer */
  544.         Srec.Ptr = Srec.Buf + (AddrCnt & 3L);    /*  aligned.   */
  545.     }
  546.     StartAddr = TempAddr = AddrCnt;
  547.  
  548.     } else if (AddrCnt > TempAddr) {    /* AmigaDOS forward ORG */
  549.     if (OrgHigh > TempAddr) {    /* Previous backward ORG */
  550.         if (AddrCnt < OrgHigh)
  551.         templong = AddrCnt;    /* Within previous range */
  552.         else
  553.         templong = OrgHigh;    /* Beyond previous range */
  554.         i = (int) (templong & 3L);    /* Alignment factor */
  555.         templong -= TempAddr;    /* Number of bytes to skip */
  556.         LenPtr = NULL;
  557.         if (Srec.Ptr > Srec.Buf)    /* Flush the buffer. */
  558.         write (Srec.fd, Srec.Buf, Srec.Ptr - Srec.Buf);
  559.         lseek (Srec.fd,templong-(long)i,1);    /* Skip written data. */
  560.         if (i) {        /* If skip isn't to long-word, */
  561.         read(Srec.fd,Srec.Buf,(long)i);    /*  move ahead */
  562.         lseek(Srec.fd, -((long) i), 1);    /*  to keep    */
  563.         }                    /*  the buffer */
  564.         Srec.Ptr = Srec.Buf + (long) i;    /*  aligned.   */
  565.         TempAddr += templong;
  566.         StartAddr = TempAddr;
  567.     }
  568.     while (TempAddr < AddrCnt) {    /* Extend with binary zeros. */
  569.         xputc (0, &Srec);
  570.         TempAddr++;
  571.     }
  572.     }
  573. }
  574.  
  575.  
  576.  
  577. DumpSdata (f) register struct fs *f;
  578. /* Writes an object code record. */
  579. {
  580.     register long CheckSum, templong;
  581.     register char *s;
  582.     register int  i;
  583.  
  584.     if (!SFormat) {
  585.     if (AddrCnt < OrgHigh) {    /* If we did a backwards ORG, */
  586.         LenPtr = NULL;        /*  we have to fix things up. */
  587.         if (f->Ptr > f->Buf)    /* Flush the buffer. */
  588.         write (f->fd, f->Buf, f->Ptr - f->Buf);
  589.         lseek(f->fd,OrgSeek&~3L,0);    /* Back to high position */
  590.         if (OrgSeek & 3L) {        /* If ORG isn't to long-word,  */
  591.         read (f->fd, f->Buf, OrgSeek & 3L);    /*  move ahead */
  592.         lseek (f->fd, -(OrgSeek & 3L), 1);    /*  to keep    */
  593.         }                        /*  the buffer */
  594.         f->Ptr = f->Buf + (OrgSeek & 3L);        /*  aligned.   */
  595.         AddrCnt = StartAddr = TempAddr = OrgHigh;
  596.     }
  597.     AddrCnt = AddrBndL (AddrCnt);    /* Finish the last long word. */
  598.     templong = (AddrCnt - SectStart) >> 2;
  599.     if ((s = LenPtr) == NULL) {
  600.         if (f->Ptr > f->Buf)        /* Flush the buffer. */
  601.         write (f->fd, f->Buf, f->Ptr - f->Buf);
  602.         CheckSum = lseek (f->fd, 0L, 1);    /* Remember position. */
  603.         lseek (f->fd, LenPos, 0);        /* Put hunk length here. */
  604.         s = f->Buf;
  605.     }
  606.     *s++ = (char) (templong >> 24);
  607.     *s++ = (char) (templong >> 16);
  608.     *s++ = (char) (templong >> 8);
  609.     *s++ = (char) templong;
  610.     if (LenPtr == NULL) {
  611.         write (f->fd, f->Buf, 4);
  612.         lseek (f->fd, CheckSum, 0);    /* Go back to where we were. */
  613.         f->Ptr = f->Buf;
  614.     }
  615.     DumpRel (f);        /* Write relocation information. */
  616.     templong = HunkEnd;
  617.     xputl (f, templong);    /* End of the hunk */
  618.     TempAddr = AddrCnt;
  619.     return;
  620.     }
  621.  
  622.     if (Sindex == 0)
  623.      return;        /* There's nothing to dump. */
  624.  
  625.     xputs (f, "S2");
  626.     templong = Sindex + 4;    /* Record length */
  627.     LongPut (f, templong, 1);
  628.     CheckSum = templong;    /* Initialize CheckSum. */
  629.  
  630.     LongPut (f, StartAddr, 3);    /* Address */
  631.     CheckSum += (StartAddr >> 16) & 0x00FFL;
  632.     CheckSum += (StartAddr >> 8) & 0x00FFL;
  633.     CheckSum += StartAddr & 0x00FFL;
  634.  
  635.     for (i = 0; i < Sindex; i++) {
  636.     templong = Sdata[i];
  637.     LongPut (f, templong, 1);    /* Object code */
  638.     CheckSum += templong;
  639.     }
  640.     CheckSum = ~CheckSum;    /* Complement the checksum. */
  641.     LongPut (f, CheckSum, 1);
  642.     xputs (f, "\n");
  643.  
  644.     StartAddr += Sindex;
  645.     TempAddr = StartAddr;
  646.     Sindex = 0;
  647. }
  648.  
  649.  
  650.  
  651. PutRel (addr, hunk, size) long addr, hunk; int size;
  652. /* Build a relocation entry if necessary. */
  653. {
  654.     register struct RelTab *rel;
  655.  
  656.     if (!Pass2)
  657.     return;                /* Pass 2 only */
  658.     if (SFormat)
  659.     return;                /* Not for S-format! */
  660.     if (hunk == ABSHUNK)
  661.     return;                /* Absolute */
  662.     if (HunkType == HunkBSS)
  663.     return;                /* Not for BSS hunks! */
  664.  
  665.     rel = RelLim;            /* Pointer to new entry. */
  666.     RelLim++;                /* Bump limit pointer. */
  667.     if (((char *) RelLim - (char *) RelCurr) > CHUNKSIZE) {
  668.     rel = (struct RelTab *) malloc ((unsigned) CHUNKSIZE);
  669.     if (rel == NULL)
  670.         quit_cleanup ("Out of memory!\n");
  671.     RelCurr->Link = rel;        /* Link from previous chunk */
  672.     RelCurr = rel;            /* Make the new chunk current. */
  673.     RelCurr->Link = NULL;        /* Clear forward pointer. */
  674.     rel++;                /* Skip over pointer entry. */
  675.     RelLim = rel;            /* New table limit */
  676.     RelLim++;            /* Bump it. */
  677.     }
  678.     if (RelLast != NULL)
  679.     RelLast->Link = rel;        /* Link from previous entry */
  680.     rel->Link = NULL;            /* End of the chain (so far) */
  681.     rel->Offset = addr;            /* Offset */
  682.     rel->Hunk = hunk;            /* Hunk number */
  683.     rel->Size = size;            /* Size */
  684.     RelLast = rel;            /* Pointer to last entry in chain */
  685.  
  686.     if (hunk < 0)            /* Count entries by type. */
  687.     NumRExt++;
  688.     else if (size == Long)
  689.     NumR32++;
  690.     else if (size == Word)
  691.     NumR16++;
  692.     else
  693.     NumR8++;
  694. }
  695.  
  696.  
  697.  
  698. DumpRel (f) struct fs *f;
  699. /* Dump relocation information to the object file. */
  700. {
  701.     register struct SymTab *sym;
  702.     register struct RelTab *rel, *rel2;
  703.     int  j, size, num, donexhdr, secthlin;
  704.     long currhunk, nexthunk, templong;
  705.     char *s, *t;
  706.  
  707.     secthlin = LineCount;        /* Current section ends here */
  708.     if ((Dir == Section)        /*   unless we're starting   */
  709.     || (Dir == CSeg)            /*      a new section.       */
  710.     || (Dir == DSeg)
  711.     || (Dir == BSS))
  712.     secthlin--;            /* Then it ends at previous line. */
  713.  
  714.     if (SFormat)
  715.     return;                /* S-format is absolute! */
  716.  
  717.     while (1) {
  718.     if ((num = NumR32) != 0) {
  719.         size = Long;        /* Do 32-bit fields */
  720.         templong = HunkR32;
  721.         NumR32 = 0;            /* ...but only once. */
  722.     } else if ((num = NumR16) != 0) {
  723.         size = Word;        /* Then do 16-bit fields. */
  724.         templong = HunkR16;
  725.         NumR16 = 0;
  726.     } else if ((num = NumR8) != 0) {
  727.         size = Byte;        /* Finally do 8-bit fields. */
  728.         templong = HunkR8;
  729.         NumR8 = 0;
  730.     } else
  731.         break;            /* We're all done. */
  732.  
  733.     xputl (f, templong);        /* Record type */
  734.  
  735.     currhunk = 32767;
  736.     num = 0;
  737.     if (rel = RelStart)    /* If we have anything, */
  738.         rel++;        /*  skip over the first chunk's link. */
  739.     while (rel) {
  740.         if ((rel->Size == size) && (rel->Hunk >= 0)) {
  741.         if (rel->Hunk < currhunk) {
  742.             currhunk = rel->Hunk;    /* Lowest hunk number */
  743.             num = 1;            /* Reset counter. */
  744.         } else if (rel->Hunk == currhunk) {
  745.             num++;            /* Count entries. */
  746.         }
  747.         }
  748.         rel = rel->Link;
  749.     }
  750.     while (num > 0) {    /* Repeat for all hunk references. */
  751.         templong = num;
  752.         xputl (f, templong);    /* Number of entries */
  753.         xputl (f, currhunk);    /* Hunk number */
  754.         nexthunk = 32767;
  755.         num = 0;            /* Count for next hunk */
  756.         if (rel = RelStart)
  757.         rel++;
  758.         while (rel) {
  759.         if ((rel->Size == size) && (rel->Hunk >= 0)) {
  760.             if (rel->Hunk < currhunk) {
  761.             rel = rel->Link;    /* We already wrote it. */
  762.             continue;
  763.             } else if (rel->Hunk == currhunk) {
  764.             xputl (f, rel->Offset - SectStart);
  765.             } else if (rel->Hunk < nexthunk) {
  766.             nexthunk = rel->Hunk;    /* Next hunk number */
  767.             num = 1;        /* Reset counter. */
  768.             } else if (rel->Hunk == nexthunk) {
  769.             num++;            /* Count entries. */
  770.             }
  771.         }
  772.         rel = rel->Link;
  773.         }
  774.         currhunk = nexthunk;    /* Get ready for next hunk. */
  775.     }
  776.     xputl (f, 0L);        /* End of relocation information */
  777.     }
  778.  
  779.     donexhdr = FALSE;        /* We haven't written hunk_ext yet. */
  780.  
  781.     sym = SymChunk = SymStart;
  782.     sym++;
  783.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  784.     while (sym) {
  785.     if (sym->Flags & 2) {        /* Scan for XDEF symbols. */
  786.         j = sym->Defn;    /* Defined in current section? */
  787.         if ((j >= SectLine) && (j <= secthlin)) {
  788.         if (!donexhdr) {
  789.             templong = HunkExt;    /* We haven't done header yet. */
  790.             xputl (f, templong);
  791.             donexhdr = TRUE;
  792.         }
  793.         if ((sym->Hunk & 0x0000FFFFL) == ABSHUNK)
  794.             templong = 0x02000000;
  795.         else
  796.             templong = 0x01000000;        /* Flags */
  797.         DumpName (f, sym->Nam, templong);    /* Symbol */
  798.         xputl (f, sym->Val - SectStart);    /* Offset */
  799.         }
  800.     }
  801.     sym = NextSym (sym);
  802.     }
  803.  
  804.     if (NumRExt != 0) {            /* External references (XREF) */
  805.     if (!donexhdr) {
  806.         templong = HunkExt;        /* We haven't done header yet. */
  807.         xputl (f, templong);
  808.         donexhdr = TRUE;
  809.     }
  810.     if (rel = RelStart)
  811.         rel++;
  812.     while (rel) {
  813.         if (rel->Hunk < 0) {
  814.         size = rel->Size;
  815.         if (size == Long)
  816.             templong = 0x81000000L;    /* ext_ref32 */
  817.         else if (size == Word)
  818.             templong = 0x83000000L;    /* ext_ref16 */
  819.         else
  820.             templong = 0x84000000L;    /* ext_ref8 */
  821.         s = (char *) ~(rel->Hunk);
  822.         DumpName (f, s, templong);    /* Flags and symbol */
  823.         templong = 1;
  824.         rel2 = rel->Link;
  825.         while (rel2) {
  826.             if ((rel2->Hunk == rel->Hunk) && (rel2->Size == size))
  827.             templong++;        /* Number of times */
  828.             rel2 = rel2->Link;        /*  symbol occurs  */
  829.         }
  830.         xputl (f, templong);
  831.         rel2 = rel;            /* Now go back and  */
  832.         while (rel2) {            /*  write them out. */
  833.             if ((rel2->Hunk==rel->Hunk) && (rel2->Size==size)) {
  834.             xputl(f, rel2->Offset - SectStart); /* Offset */
  835.             if (rel2 != rel)    /* Kill hunk so we    */
  836.                 rel2->Hunk = 0;    /*  don't do it again */
  837.             }                /*  (we're done with  */
  838.             rel2 = rel2->Link;        /*  the table anyway). */
  839.         }
  840.         }
  841.         rel = rel->Link;
  842.     }
  843.     NumRExt = 0;
  844.     }
  845.     if (donexhdr)
  846.     xputl (f, 0L);            /* End of external information */
  847.  
  848.     if (DumpSym) {            /* Dump the symbol table. */
  849.     donexhdr = FALSE;
  850.     sym = SymChunk = SymStart;
  851.     sym++;
  852.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  853.     for (; sym; sym = NextSym (sym)) {
  854.         s = DumpSymList;
  855.         if (*s) {        /* Select by prefix. */
  856.         if (*s == '!')
  857.             s++;    /* Symbols must NOT match prefix. */
  858.         t = sym->Nam;
  859.         j = TRUE;    /* Assume we have a match. */
  860.         while (*s) {
  861.             if (*s++ != *t++) {
  862.             j = FALSE;    /* Symbol does not match prefix. */
  863.             break;
  864.             }
  865.         }
  866.         if (DumpSymList[0] == '!') {
  867.             if (j)
  868.             continue;
  869.         } else {
  870.             if (!j)
  871.             continue;
  872.         }
  873.         }
  874.         if ((sym->Hunk & 0x0000FFFFL) == CurrHunk) {
  875.         j = sym->Flags & 0x7F;        /* Ignore PUBLIC flag. */
  876.         if ((j == 0) || (j == 2)) {    /* Defined, may be XDEF. */
  877.             if ((sym->Defn >= SectLine) && (sym->Defn <= secthlin)) {
  878.             if (!donexhdr) {    /* It's in current SECTION. */
  879.                 templong = HunkSym;
  880.                 xputl (f, templong);    /* Write header */
  881.                 donexhdr = TRUE;        /* if necessary. */
  882.             }
  883.             DumpName (f, sym->Nam, 0L);    /* Symbol */
  884.             xputl(f, sym->Val - SectStart);    /* Offset */
  885.             }
  886.         }
  887.         }
  888.     }
  889.     if (donexhdr)
  890.         xputl (f, 0L);        /* End of symbol table dump */
  891.     }
  892.  
  893.     rel = RelStart->Link;
  894.     while (rel != NULL) {
  895.     rel2 = rel;
  896.     rel = rel2->Link;
  897.     free (rel2);            /* Free all but the first chunk. */
  898.     }
  899.     RelCurr = RelStart;            /* The first chunk is current. */
  900.     RelCurr->Link = NULL;        /* Unlink additional chunks. */
  901.     RelLast = NULL;            /* There are no entries left. */
  902.     RelLim = RelStart;
  903.     RelLim++;                /* First unused space */
  904. }
  905.  
  906.  
  907.  
  908. EndSdata (f, addr) struct fs *f; long addr;
  909. /* Write end record to object file. */
  910. {
  911.     register long checksum, templong;
  912.  
  913.     if (SFormat) {
  914.     DumpSdata (f);            /* Write any remaining data. */
  915.     xputs (f, "S804");        /* Record header */
  916.     checksum = 4;
  917.     LongPut (f, addr, 3);        /* Transfer address */
  918.     checksum += (addr >> 16) & 0x00FFL;
  919.     checksum += (addr >> 8) & 0x00FFL;
  920.     checksum += addr & 0x00FFL;
  921.     checksum = ~checksum;
  922.     LongPut (f, checksum, 1);    /* Checksum */
  923.     xputs (f, "\n");
  924.     } else {
  925.     if (HunkType != HunkNone) {
  926.         DumpSdata (f);        /* Last hunk's data */
  927.     }
  928.     }
  929. }
  930.  
  931.  
  932.  
  933. DumpName (f, name, flags) struct fs *f; char *name; long flags;
  934. /* Writes a name preceded by a long word containing the
  935.     length of the name in long words.  The length word has
  936.     the contents of "flags" ORed into it.  The name is padded
  937.     with binary zeros to the next long word boundary. */
  938. {
  939.     register int  i;
  940.     register long templong;
  941.  
  942.     i = strlen (name);
  943.     templong = (i + 3) >> 2;    /* Length of name (long words) */
  944.     templong |= flags;        /* Add flag bits. */
  945.     xputl (f, templong);    /* Write length and flags. */
  946.     xputs (f, name);        /* Write the name itself. */
  947.     while (i & 3) {
  948.     xputc ('\0', f);    /* Pad the last word. */
  949.     i++;
  950.     }
  951. }
  952.  
  953.  
  954.  
  955. LongPut (f, data, length) struct fs *f; long data; int length;
  956. /* Writes to file "f" the hexadecimal interpretation of
  957.     the bytes in "data".  The number of bytes written
  958.     (two hex digits per byte) is given in "length" -
  959.     if less than 4, only low-order bytes are written. */
  960. {
  961.     register int i, j;
  962.     register char *t;
  963.     char xstr[9];
  964.  
  965.     t = xstr;
  966.     for (i = length * 8 - 4; i >= 0; i -= 4) {
  967.     j = (int) ((data >> i) & 0x0FL);
  968.     *t++ = (char) ((j > 9) ? (j - 10 + 'A') : (j + '0'));
  969.     }
  970.     *t = '\0';
  971.     xputs (f, xstr);
  972. }
  973.  
  974.  
  975.  
  976. xopen (name, f, desc) char *name; struct fs *f; char *desc;
  977. /* Opens the output file whose name is in "name",
  978.     setting up the file structure pointed to by "f".
  979.     This routine first allocates a file buffer -
  980.     if unsuccessful, it calls quit_cleanup.
  981.     Otherwise, it opens the file - if unsuccessful,
  982.     displays an error message using "desc" and returns TRUE.
  983.     If the file is successfully opened, this routine returns FALSE. */
  984. {
  985.     if ((f->Buf = (char *) malloc (BUFFSIZE)) == NULL)
  986.     quit_cleanup ("Out of memory!\n");
  987.     if ((f->fd = creat (name, 1)) < 0) {
  988.     fprintf (stderr, "Unable to open %s file.\n", desc);
  989.     f->fd = NULL;
  990.     return (TRUE);
  991.     }
  992.     f->Ptr = f->Buf;
  993.     f->Lim = f->Buf + BUFFSIZE;
  994.     return (FALSE);
  995. }
  996.  
  997.  
  998.  
  999. xputs (f, s) struct fs *f; register char *s;
  1000. /* Writes the string pointed to by "s"
  1001.     to the output file whose structure is pointed to by "f". */
  1002. {
  1003.     register char *t, *l;
  1004.  
  1005.     t = f->Ptr;        /* Current position (use registers for speed) */
  1006.     l = f->Lim;        /* End of buffer */
  1007.  
  1008.     while (*s) {
  1009.     *t++ = *s++;
  1010.     if (t >= l) {
  1011.         write (f->fd, f->Buf, t - f->Buf);    /* Flush the buffer. */
  1012.         if (f == &Srec)
  1013.         LenPtr = NULL;    /* Hunk length is no longer in buffer. */
  1014.         t = f->Buf;                /* Reset pointer. */
  1015.     }
  1016.     }
  1017.     f->Ptr = t;                    /* Update pointer. */
  1018. }
  1019.  
  1020.  
  1021.  
  1022. xputl (f, data) register struct fs *f; register long data;
  1023. /* Writes to file "f" the contents of the long word in "data". */
  1024. {
  1025.     xputc ((char) (data >> 24), f);
  1026.     xputc ((char) (data >> 16), f);
  1027.     xputc ((char) (data >> 8), f);
  1028.     xputc ((char) data, f);
  1029. }
  1030.  
  1031.  
  1032.  
  1033. xputc (byte, f) char byte; register struct fs *f;
  1034. /* Writes the byte contained in "byte" to file "f". */
  1035. {
  1036.     register char *t;
  1037.  
  1038.     t = f->Ptr;        /* Current position (use register for speed) */
  1039.  
  1040.     *t++ = byte;
  1041.     if (t >= f->Lim) {
  1042.     write (f->fd, f->Buf, t - f->Buf);    /* Flush the buffer. */
  1043.     if (f == &Srec)
  1044.         LenPtr = NULL;    /* Hunk length is no longer in buffer. */
  1045.     t = f->Buf;                /* Reset pointer. */
  1046.     }
  1047.     f->Ptr = t;                    /* Update pointer. */
  1048. }
  1049.  
  1050.  
  1051.  
  1052. xclose (f) struct fs *f;
  1053. /* Closes the output file whose structure is pointed to by "f".
  1054.     The buffer is flushed if necessary, then freed.        */
  1055. {
  1056.     if (f->Ptr > f->Buf)
  1057.     write (f->fd, f->Buf, f->Ptr - f->Buf);    /* Flush the buffer. */
  1058.     close (f->fd);                /* Close the file. */
  1059.     f->fd = NULL;
  1060.     free (f->Buf);                /* Free the buffer. */
  1061.     f->Buf = NULL;
  1062. }
  1063.  
  1064.  
  1065.  
  1066. Error (pos, errornum) int pos, errornum;
  1067. /* Displays error message #errornum.  If this is the first error for
  1068.     the current line, the line itself is displayed, preceded by a
  1069.     message giving the current position in the current module.
  1070.     If the line is in a macro or include file, the position in
  1071.     each nested module is given, working out to the source file.
  1072.     A flag is placed under the column indicated by "pos".       */
  1073. {
  1074.     register int i;
  1075.     int dummy;
  1076.  
  1077.     if (!Pass2 && (errornum != NoIncl)) {
  1078.     if (IncStart != 0) {            /* Don't skip this     */
  1079.         IncStart = 0;            /*  INCLUDE file in    */
  1080.         if (SkipLim->Set1 != NULL) {    /*  pass 2 - we must   */
  1081.         SetFixLim = SkipLim->Set1;    /*  re-read it to      */
  1082.         SetFixLim++;            /*  report its errors. */
  1083.         }
  1084.     }
  1085.     return;                /* Report during pass 2 only. */
  1086.     }
  1087.     if (ErrLim < ERRMAX) {        /* Save error data. */
  1088.     ErrCode[ErrLim] = errornum;
  1089.     ErrPos[ErrLim] = pos;
  1090.     ErrLim++;
  1091.     }
  1092.     if (ErrLim == 1)        /* If this is the first error for this line, */
  1093.     DisplayLine (dummy);    /*  display the line and its number(s). */
  1094.     printf ("\t");
  1095.     for (i = 0; i < pos; i++)
  1096.     if (Line[i] == '\t')
  1097.         printf ("\t");
  1098.     else
  1099.         printf(" ");        /* Space over to error column. */
  1100.     printf ("^ %s\n",errmsg[errornum]);    /* Error flag and message */
  1101.     ErrorCount++;            /* Count errors. */
  1102. }
  1103.  
  1104.  
  1105.  
  1106. DisplayLine (dummy) int dummy;
  1107. /* Displays the current line and its position
  1108.     in all current files - used by Error, etc. */
  1109. {
  1110.     register struct InFCtl *inf;
  1111.     register int i;
  1112.  
  1113.     printf ("\n");
  1114.     for (i = InFNum, inf = InF; i >= 0; i--, inf++) {    /* Nested? */
  1115.     if (inf->UPtr == 0)
  1116.         printf ("%s", inf->NPtr);        /* Module name */
  1117.     else
  1118.         printf ("(user macro)");        /* We're in a user macro. */
  1119.     printf (" line %d\n", inf->Line);    /* Line number in module */
  1120.     }
  1121.     printf ("%5d   %s\n", LineCount, Line);    /* The line itself */
  1122. }
  1123.